package cn.xxstudy.visitshop.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import cn.xxstudy.visitshop.R
import cn.xxstudy.visitshop.databinding.ItemInterviewBinding
import cn.xxstudy.visitshop.databinding.ItemTrainBinding
import cn.xxstudy.visitshop.ui.activity.main.fragment.train.model.TrainData

/**
 * @author ：DELL on 2021/3/26 .
 * @packages ：cn.xxstudy.visitshop.adapter .
 * TODO:一句话描述
 */
class TrainPagingDataAdapter :
    PagingDataAdapter<TrainData, TrainPagingDataAdapter.TrainPagingDataViewHolder>(diffCallback) {

    companion object {
        private val diffCallback = object : DiffUtil.ItemCallback<TrainData>() {
            override fun areItemsTheSame(oldItem: TrainData, newItem: TrainData): Boolean {
                return oldItem.trainId == newItem.trainId
            }

            override fun areContentsTheSame(oldItem: TrainData, newItem: TrainData): Boolean {
                return oldItem == newItem
            }
        }
    }

    class TrainPagingDataViewHolder(rootView: View) : RecyclerView.ViewHolder(rootView)

    override fun onBindViewHolder(holder: TrainPagingDataViewHolder, position: Int) {
        val binding = DataBindingUtil.getBinding<ItemTrainBinding>(holder.itemView)
        binding?.trainData = getItem(position)
        binding?.executePendingBindings()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TrainPagingDataViewHolder {
        val binding = DataBindingUtil.inflate<ItemTrainBinding>(
            LayoutInflater.from(parent.context),
            R.layout.item_train, parent, false
        )
        return TrainPagingDataViewHolder(binding.root)
    }
}